package com.lsmw.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsmw.management.entity.LsHotelInfo;
import com.lsmw.management.entity.LsRoom;
import com.lsmw.management.mapper.LsRoomMapper;
import com.lsmw.management.service.LsRoomService;
import com.lsmw.common_service.exceptionhandler.IdIllegalException;
import com.lsmw.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 二号男嘉宾
 * @since 2020-12-14
 */
@Slf4j
@Service
public class LsRoomServiceImpl extends ServiceImpl<LsRoomMapper, LsRoom> implements LsRoomService {

//    @Resource
//    LsRoomMapper lsRoomMapper;

    @Override
    public R getRoomListByHotelId(String id) {
        log.info("查询了" + id + "的房间信息");
        return R.ok().data("row", this.list(new LambdaQueryWrapper<LsRoom>().eq(LsRoom::getHotelId, id)));
    }

    @Override
    public R addRoomByHotelId(LsRoom lsRoom) {
        if (baseMapper.getHotelById(new LambdaQueryWrapper<LsHotelInfo>().eq(LsHotelInfo::getHotelId, lsRoom.getHotelId())) != null) {
            return this.save(lsRoom) ? R.created() : R.invalidRequest();
        }
        return R.invalidRequest().data("msg","请求有误，请检查酒店是否存在！");
    }

    @Override
    public R addRoomByHotelName(LsRoom lsRoom) {
        if (baseMapper.getHotelByName(new LambdaQueryWrapper<LsHotelInfo>().eq(LsHotelInfo::getName, lsRoom.getHotelName())) != null) {
            lsRoom.setHotelId(baseMapper.getHotelById(new LambdaQueryWrapper<LsHotelInfo>().eq(LsHotelInfo::getName, lsRoom.getHotelName())).getHotelId());
            return this.save(lsRoom) ? R.created() : R.invalidRequest();
        }
        return R.invalidRequest().data("msg","请求有误，请检查酒店是否存在！");
    }

    @Override
    public R updateRoomById(String id, LsRoom lsRoom) {
        try {
            lsRoom.setRoomId(Long.valueOf(id));
        } catch (Exception e) {
            throw new IdIllegalException(404, "修改房间失败id有误，请检查id值或联系管理员");
        }
        return this.updateById(lsRoom) ? R.created() : R.notFound();
    }

    @Override
    public R removeRoomById(String id) {
        return this.removeById(id) ? R.ok() : R.invalidRequest();
    }
}
